package com.bugpool.leilema.tc.service.impl;

import com.bugpool.leilema.tc.bean.entity.SysDept;
import com.bugpool.leilema.tc.bean.req.SysDeptReq;
import com.bugpool.leilema.tc.mapper.DeptMapper;
import com.bugpool.leilema.tc.service.DeptService;
import com.bugpool.leilema.tc.util.TreeUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author wh
 * @Description
 * @Date 2023/6/8 10:58
 * @Version 1.0
 **/
@Service
public class DeptServiceImpl implements DeptService {

    @Resource
    private DeptMapper deptMapper;


    @Override
    public List<SysDept> list(SysDeptReq sysDeptReq) {
        List<SysDept> sysDeptList = deptMapper.list(sysDeptReq);
        List<SysDept> build = TreeUtil.build(sysDeptList, 0L);
        return build;
    }

    @Override
    public SysDept getDeptById(Long deptId) {
        return deptMapper.selectById(deptId);
    }

    @Override
    public Boolean addDept(SysDept sysDept) {
        return deptMapper.insert(sysDept) == 1;
    }

    @Override
    public Boolean updateDept(SysDept sysDept) {
        return deptMapper.updateById(sysDept) == 1;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteDeptByIds(List<Long> deptIds) {
        for (Long deptId : deptIds) {
            deptMapper.deleteById(deptId);
        }

        return true;
    }
}
